package cn.lili.modules.promotion.entity.vos;

import cn.lili.modules.promotion.entity.dos.BlindboxPrizePaper;
import cn.lili.modules.promotion.entity.dos.BlindboxPrizeRecord;
import cn.lili.modules.promotion.entity.dos.BlindboxRule;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author xiangqian
 * @Date 2023/10/4 18:28
 */
@JsonInclude(JsonInclude.Include.ALWAYS)
@Data
@ApiModel(value = "盲盒活动详情")
@ToString(callSuper = true)
@NoArgsConstructor
public class BlindboxVO {

    @ApiModelProperty(name = "已发抽奖卷", notes = "")
    private Integer beforeNumber;

    @ApiModelProperty(name = "剩余抽奖卷", notes = "")
    private Integer endNumber;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "抽奖时间", hidden = true)
    private Date prizeTime;

    @ApiModelProperty(name = "奖品名称", notes = "")
    private String prizeName;

    @ApiModelProperty(name = "我的抽奖卷", notes = "")
    private List<String> prizeCode;

    @ApiModelProperty(name = "昵称", notes = "")
    private String nickName;

    @ApiModelProperty(name = "奖品图片", notes = "")
    private String prizeImage;

    /**
     * 购买次数（购买多少次算一次奖卷）
     */
    @ApiModelProperty(name = "购买次数（购买多少次算一次奖卷）", notes = "")
    private Integer ruleBuyNum;

    /**
     * 我的购买次数
     */
    @ApiModelProperty(name = "我的购买次数", notes = "")
    private Integer myRuleBuyNum;


    /**
     * 初始化已抽奖卷
     *
     * @param blindboxRule 盲盒规则
     * @param prizeNumber  已抽奖卷
     * @param blindboxVO   赋值对象
     */
    public void initPrizeNumber(BlindboxRule blindboxRule, Integer prizeNumber, BlindboxVO blindboxVO) {
        if (blindboxRule == null) {
            return;
        }
        Integer endNumber = blindboxRule.getRuleNum() - prizeNumber;
        //设置已发抽奖卷
        blindboxVO.setBeforeNumber(prizeNumber >= blindboxRule.getRuleNum() ? blindboxRule.getRuleNum() : prizeNumber);
        //设置剩余抽奖卷
        blindboxVO.setEndNumber(endNumber <= 0 ? 0 : endNumber);
        //设置奖品名称
        blindboxVO.setPrizeName(blindboxRule.getPrizeName());
        //设置奖品图片
        blindboxVO.setPrizeImage(blindboxRule.getPrizeImage());
    }


    /**
     * 初始化我的奖卷
     *
     * @param blindboxPrizePaperList 我的奖卷
     * @param blindboxVO             赋值对象
     */
    public void initPrizeCode(List<BlindboxPrizePaper> blindboxPrizePaperList, BlindboxVO blindboxVO) {
        if (blindboxPrizePaperList == null || blindboxPrizePaperList.size() < 1) {
            return;
        }
        List<String> list = Optional.ofNullable(blindboxPrizePaperList).orElse(new ArrayList<>()).stream().map(BlindboxPrizePaper::getCode).collect(Collectors.toList());
        blindboxVO.setPrizeCode(list);
    }
}
